<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Mail;
use Illuminate\Http\Request;

use App\Http\Requests;
use Think\Storage;

class StudentController extends Controller
{
    public function queue()
    {
        dispatch(new SendEmail('1379700165@qq.com'));
    }

    //错误
    public function error()
    {
        //$student=DB::table('student') 1001;
        $student=null;
        if($student==null)
        {
            abort('500');
        }

        $name="sean";
        var_dump($name);
        return view('student.error');
        Log::info('这是一个inof级别的日志');
        Log::warning('这是一个warning级别的日志');
        Log::error('这是一个数组',['name'=>'sean','age'=>18]);
    }
    //缓存
    public function cache1()
    {
        //put()
        Cache::put('key1','val1',10);

        //add()  key值存在时返回布尔值false 不存在时添加,返回true
        $bool=Cache::add('key2','val2',10);
        var_dump($bool);

        //forever()
        Cache::forever('key3','val3');

        //has()
        if(Cache::has('key1'))
        {
            $val=Cache::get('key1');
            var_dump($val);
        }
        else
        {
            echo "No";
        }
    }

    public function cache2()
    {
        //get()
        $val=Cache::get('key3');

        //pull()  取出后删除
        $val=Cache::pull('key3');
        var_dump($val);

        //forget()  删除
        $bool=Cache::forget('key1');
        var_dump($bool);
    }

    //邮件发送
    public function mail()
    {
        //发送纯文本邮件
        /*
        Mail::raw('邮件内容',function($message){
            $message->from('xiaoyan37','慕课网');
            $message->subject('邮件主题 测试');
            $message->to('1379700165@qq.com');
        });
        */
        Mail::send('student.mail',[
            'name'=>'sean',
            'age'=>18,
            //'student'=>$student (对象)
        ],function($message){
            $message->to('1379700165@qq.com');
        });
    }
    //文件上传
    public function upload(Request $request)
    {
        if($request->isMethod('POST'))
        {
            $file=$request->file('source');
            //文件是否上传成功
            if($file->isValid())
            {
                //原文件名
                $originaName=$file->getClientOriginalName();
                //扩展名
                $ext=$file->getClientOriginalExtension();
                //MimeType
                $type=$file->getClientMimeType();
                //临时绝对路径
                $realPath=$file->getRealPath();
                $filename=date('Y-m-d-H-i-s').'-'.uniqid().'.'.$ext;
                $bool=Storage::disk('uploads')->put($filename,file_get_contents($realPath));
                var_dump($bool);
            }

         }
        return view('student.upload');
    }
}
